package com.xuxin.queue.activity;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.queue.R;
import com.xuxin.queue.base.BaseActivity;
import com.xuxin.queue.utils.ACache;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2021/6/17 0017
 */
public class ConfigureActivity extends BaseActivity {
    private ACache aCache;
    @BindView(R.id.configure_url)
    EditText configure_url;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_configure);
    }

    @Override
    public void initView() {
        aCache = ACache.get(context);
        String url = aCache.getAsString("URL");
        if (!StringUtils.isEmpty(url)) {
            configure_url.setText(url);
            LogUtils.e("有保存的url");
        } else {
            LogUtils.e("没有保存的url");
        }
        configure_url.setHorizontallyScrolling(false);
        configure_url.setMaxLines(Integer.MAX_VALUE);
        configure_url.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    configure();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.configure_finish})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.configure_finish) {
            configure();
        }
    }

    /**
     * 配置完成
     */
    private void configure() {
        String url = configure_url.getText().toString().trim();
        if (!StringUtils.isEmpty(url)) {
            if (!url.equals(aCache.getAsString("URL"))) {
                aCache.put("URL", url);
            } else {
                LogUtils.e("url地址一样无需保存");
            }
            ActivityUtils.startActivity(QueueActivity.class);
            finish();
        } else {
            ToastUtils.showShort("url地址不能为空");
        }
    }
}